﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SharePoint;
using System.Reflection;
using System.Diagnostics;
using System.IO;
using IntelligentDecisioning.SharePoint.Helpers;

namespace IntelligentDecisioning.SharePoint.FeatureReceivers.Core
{
    /// <summary>
    /// Our core features are scoped at site collection level.
    /// </summary>
    public class CoreFeatureReceiver: SPFeatureReceiver
    {
        //TOD: make this class extend idVabeFeature receiver and use the framework to provision itself

        protected IDListHelper _listHelper;

        /// <summary>
        /// We perform a number of actions when our cour feature is activated on
        /// a site to prepare for intelligent Decisioning features including:
        /// 
        ///  - Configuration System - We add two hidden lists and an event receiever to 
        ///  store and remove our features configuration information automatically.
        /// 
        ///  - Logging - A central logging framework to track the actions taken by ID
        ///  features, caputre error information etc
        /// 
        /// </summary>
        /// <param name="properties"></param>
        public override void FeatureActivated(SPFeatureReceiverProperties properties)
        {
            SPSite currentSiteCollection = (SPSite)properties.Feature.Parent; 
            SPWeb currentSite = currentSiteCollection.RootWeb;
            

          // Add the configuration framework 
            SharePointConfiguration.AddFeatureConfigurationFramework(currentSiteCollection);

          // Add our configuration data

#if DEBUG
            foreach (String sr in Assembly.GetExecutingAssembly().GetManifestResourceNames())
            {
                Debug.WriteLine(sr);
            }
#endif
            // Read our config settings
            Stream s = Assembly.GetExecutingAssembly().GetManifestResourceStream("IntelligentDecisioning.SharePoint.FeatureReceivers.Core.CoreFeature_ConfigurationMetadata.xml");
            StreamReader sw = new StreamReader(s);
            string xmlConfig = sw.ReadToEnd();

            //TODO: Register our feature
            //TODO: Store or configuration
           // SharePointConfiguration.RegisterFeatureConfiguration(properties.Feature.DefinitionId, properties.Feature.Definition.Name
            currentSite.Update();

        }


        public override void FeatureDeactivating(SPFeatureReceiverProperties properties)
        {
            SPSite currentSiteCollection = (SPSite)properties.Feature.Parent;
            SPWeb currentSite = currentSiteCollection.RootWeb;
           
         // Add our configuration  
            SharePointConfiguration.RemoveFeatureConfigurationFramework(currentSiteCollection);
            currentSite.Update();
        }

        public override void FeatureInstalled(SPFeatureReceiverProperties properties)
        {
           // throw new NotImplementedException();
        }


        public override void FeatureUninstalling(SPFeatureReceiverProperties properties)
        {
            //throw new NotImplementedException();
        }

       
    }
}
